package com.foodost.cwpstrokes;

import net.youmi.android.offers.OffersManager;
import net.youmi.android.offers.PointsChangeNotify;
import net.youmi.android.offers.PointsManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

public class SettingActivity extends Activity implements OnClickListener,PointsChangeNotify{
    private static final String TAG = SettingActivity.class.getSimpleName();
    private LockImageView []lockImage;
    private CWPStrokesModel model;
    private AlertDialog dialog;
    private static final int INTEGRAL = 200;
    private int lockLevel = 0;
    private LockImageView lockView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);
        initData();
        initView();
        initAD();
    }
    
    public void initData(){
        model = new CWPStrokesModel(this);
    }
    
    public void initView(){
        lockImage = new LockImageView[11];
        lockImage[0] = (LockImageView)findViewById(R.id.level1);
        lockImage[1] = (LockImageView)findViewById(R.id.level2);
        lockImage[2] = (LockImageView)findViewById(R.id.level3);
        lockImage[3] = (LockImageView)findViewById(R.id.level4);
        lockImage[4] = (LockImageView)findViewById(R.id.level5);
        lockImage[5] = (LockImageView)findViewById(R.id.level6);
        lockImage[6] = (LockImageView)findViewById(R.id.level7);
        lockImage[7] = (LockImageView)findViewById(R.id.level8);
        lockImage[8] = (LockImageView)findViewById(R.id.level9);
        lockImage[9] = (LockImageView)findViewById(R.id.level10);
        lockImage[10] = (LockImageView)findViewById(R.id.level11);
        for(int i = 0; i < lockImage.length; i++){
            Level level = model.getLevel(this, i);
            if(level != null && level.isOpen()){
                lockImage[i].setOpen(true);
            }
            if(Utils.getOrmGrage(this) == i){
                lockImage[i].setBackgroundResource(R.drawable.bg_flag);
            }
        }
        for(LockImageView lock : lockImage){
            lock.setOnClickListener(this);
        }
    }
    
    private void initAD(){
        OffersManager.getInstance(this).onAppLaunch();
        PointsManager.getInstance(this).registerNotify(this);
    }
    
    protected void onDestroy() {
        OffersManager.getInstance(this).onAppExit();
        PointsManager.getInstance(this).unRegisterNotify(this);
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        for(LockImageView lock : lockImage){
            if(v.getId() == lock.getId()){
                lockLevel = lock.getLevel();
                lockView = lock;
                if(lock.isOpen){
                    int wordIndex = model.getWordIndexFromLevel(this, lockLevel);
                    if(wordIndex == 20*(lockLevel + 1) - 1 && Utils.getOrmGrage(this) != lockLevel){
                     model.getHanziListByGrage(this, lockLevel,true);
                    }
                    startMainActivityByGrage(lockLevel);
                }else{
                    isNotOpenShowDialog();
                }
            }
        }
    }
    
    public void startMainActivityByGrage(int grage){
        Utils.setOrmGrage(this, grage);
        Intent in = new Intent();
        in.setClass(this, MainActivity.class);
        startActivity(in);
    }
    
    public void isNotOpenShowDialog(){
        int myPointBalance = PointsManager.getInstance(this).queryPoints();
        if(myPointBalance > INTEGRAL){
            PointsManager.getInstance(this).spendPoints(INTEGRAL);
            model.getHanziListByGrage(this, lockLevel,false);
            lockView.setOpen(true);
            startMainActivityByGrage(lockLevel);
            if(dialog != null && dialog.isShowing()){
                dialog.dismiss();
            }
        }else if(myPointBalance < INTEGRAL && !lockView.isOpen()){
            if(dialog != null && dialog.isShowing()){
                dialog.dismiss();
            }
            showOneGrageFinishDialog(R.string.grage_integral_open,INTEGRAL - myPointBalance); 
        }
    }
    
    public void showOneGrageFinishDialog(int resId,int mIntegral){
        AlertDialog.Builder builder = new Builder(SettingActivity.this);
        String content = null;
        if(mIntegral > 0){
            content = SettingActivity.this.getResources().getString(resId, mIntegral);
        }
        builder.setMessage(content);

        builder.setTitle(R.string.game_new_grage);
        builder.setNegativeButton(R.string.get_integral, new DialogInterface.OnClickListener() {

         @Override
         public void onClick(DialogInterface dialog, int which) {
          dialog.dismiss();
          OffersManager.getInstance(SettingActivity.this).showOffersWall();
         }
        });
        dialog = builder.create();
        dialog.setCanceledOnTouchOutside(false);
        if(!dialog.isShowing()){
            dialog.show(); 
        }
        
    }

    @Override
    public void onPointBalanceChange(int pointsBalance) {
        Log.d(TAG, "pointsBalance = " + pointsBalance);
        Level level = model.getLevel(this, lockLevel);
        boolean isOpen = false;
        if(level != null){
            isOpen = level.isOpen();
        }
        isNotOpenShowDialog();
    };

}
